﻿/**
* @file longyi_banzidong.c
* @brief this is a brief description
* @details this is the detail description.
* @author fulong
* @data 2014/12/24
* @version 1.0
* @par Copyright(c):
* @par First build
*     version: fulong
*     data: 2014/12/24
*/
#include "longyi_banzidong.h"
#include "../../../../dev/input.h"
#include "../../action.h"
#include "../../../../lib/delay.h"

#ifdef __XT14042__
extern input_status_process status_process;
uint8 cRun_Step;
uint8 bRunning = 0;
uint8 kaliao = 0;
uint8 step_pre = 0;
uint8 second_counter = 0;
uint8 first_run = 0;

/*! \brief 检测是否踏了脚踏开关
* \param
* \retrun
*/
void CheckStart(void) {
//		启动	 停止
    if (X00_UP && (!bRunning) && (X01 && !X02 && X03 && !X04)) {   //自动启动
        bRunning = 1;       //运行标志位
        cRun_Step = 0;
        second_counter = 1;
    }
}
void longyi_banzidong(void) {
    if (first_run == 0) {
        Y01 = 1;
        Y04 = 1;
        first_run = 1;
    }
    // 没有按下急停按钮
    if (bRunning == 0) {
        CheckStart();   // 调用脚踏开关检测程序
        apply4delay(ACTION_DELAY_FLAG,  read_action_param(6) * 10);
    }
    if (second_counter == 1 && read_action_param(5) != 0 && !bRunning) {
        bRunning = 1;       //运行标志位
        cRun_Step = 0;
        apply4delay(ACTION_DELAY_FLAG,  read_action_param(6) * 10);
        second_counter = 2;
    }
    if (bRunning) {
        switch (cRun_Step) {
            case 0:
                //按下脚踏开关，夹紧气缸工作
                if (X01 && !X02 && X03 && !X04 && no_clr_get_delay_flag(ACTION_DELAY_FLAG)) {
                    cRun_Step = 1;
                    Y02 = 0;
                    Y04 = 1;
                    Y03 = 0;
                    Y00 = 1;
                    clr_delay_flag(ACTION_DELAY_FLAG3);
                    clr_delay_flag(ACTION_DELAY_FLAG);
                    apply4delay(ACTION_DELAY_FLAG1,  read_action_param(1) * 10);
                }
                break;
            case 1:
                if (!get_delay_flag(ACTION_DELAY_FLAG1)) {
                    if (!X01 && X02) {
                        apply4delay(ACTION_DELAY_FLAG,  read_action_param(2) * 10);
                        Y01 = 0;
                        cRun_Step = 10;
                        apply4delay(ACTION_DELAY_FLAG1,  read_action_param(1) * 10);
                    }
                } else {
                    step_pre = 1;
                    cRun_Step = 6;
                }
                break;
            case 10:
                //夹紧10ms后，松开关闭夹紧气缸
                if (!get_delay_flag(ACTION_DELAY_FLAG1)) {
                    if (X02 && !X01 &&  no_clr_get_delay_flag(ACTION_DELAY_FLAG)) {
                        cRun_Step = 2;
                        Y00 = 0;
                        clr_delay_flag(ACTION_DELAY_FLAG);
                        apply4delay(ACTION_DELAY_FLAG1,  read_action_param(1) * 10);
                    }
                } else {
                    step_pre = 10;
                    cRun_Step = 6;
                }
                break;
            case 2:
                //送料气缸工作
                if (!get_delay_flag(ACTION_DELAY_FLAG1)) {
                    if (X03 && !X04 && X01 && !X02) {
                        apply4delay(ACTION_DELAY_FLAG,  read_action_param(3) * 10);
                        cRun_Step = 20;
                        apply4delay(ACTION_DELAY_FLAG1,  read_action_param(1) * 10);
                    }
                } else {
                    step_pre = 2;
                    cRun_Step = 6;
                }
                break;
            case 20:
                //送料气缸工作
                if (!get_delay_flag(ACTION_DELAY_FLAG1)) {
                    if (X03 && !X04 && X01 && !X02 && no_clr_get_delay_flag(ACTION_DELAY_FLAG)) {
                        cRun_Step = 3;
                        Y02 = 1;
                        clr_delay_flag(ACTION_DELAY_FLAG);
                        apply4delay(ACTION_DELAY_FLAG1,  read_action_param(1) * 10);
                    }
                } else {
                    step_pre = 20;
                    cRun_Step = 6;
                }
                break;
            case 3: //延时cSetPara[0]*10后，夹料气缸工作
                if (!get_delay_flag(ACTION_DELAY_FLAG1)) {
                    if (!X03 && X04) {
                        apply4delay(ACTION_DELAY_FLAG,  read_action_param(0) * 10);
                        cRun_Step = 30;
                        apply4delay(ACTION_DELAY_FLAG1,  read_action_param(1) * 10);
                    }
                } else {
                    step_pre = 3;
                    cRun_Step = 6;
                }
                break;
            case 30:
                if (!get_delay_flag(ACTION_DELAY_FLAG1)) {
                    if (!X03 && X04 && no_clr_get_delay_flag(ACTION_DELAY_FLAG)) {
                        cRun_Step = 4;
                        Y01 = 1;
                        clr_delay_flag(ACTION_DELAY_FLAG);
                        apply4delay(ACTION_DELAY_FLAG,  6 * 10);
                    }
                } else {
                    step_pre = 30;
                    cRun_Step = 6;
                }

                break;
            case 4:
                //夹料约60ms后，送料出去
                if (get_delay_flag(ACTION_DELAY_FLAG)) {
                    cRun_Step = 5;
                    Y02 = 0;
                    apply4delay(ACTION_DELAY_FLAG2,   read_action_param(4) * 10);
                }
                break;
            case 5:
                if (!get_delay_flag(ACTION_DELAY_FLAG2)) {
                    if (X01 && !X02 && X03 && !X04) {
                        bRunning = 0;
                        if (second_counter == 2) second_counter = 0;
                    }
                } else {
                    Y01 = 0;
                    Y02 = 1;
                    if (kaliao < 5) {
                        apply4delay(ACTION_DELAY_FLAG1,   read_action_param(1) * 10);
                        cRun_Step = 3;
                    } else {
                        step_pre = 3;
                        cRun_Step = 6;
                    }
                    kaliao++;
                    apply4delay(ACTION_DELAY_FLAG,   read_action_param(0) * 10);
                }
                break;
            case 6:
                if ((X00_UP)) {
                    apply4delay(ACTION_DELAY_FLAG1,   read_action_param(1) * 10);
                    cRun_Step = step_pre;
                    switch (step_pre) {
                        case 3 :
                            kaliao = 0;
                            Y01 = 0;
                            Y02 = 1;
                            break;
                        default:
                            break;
                    }
                }
                break;
            default:
                bRunning = 0;
                break;
        }
    } else {
        apply4delay(ACTION_DELAY_FLAG3,    1000 * 10);
        if (get_delay_flag(ACTION_DELAY_FLAG3)) {
            Y04 = 0;
        }
    }
}
#endif
